package com.yyfax.pay.config.data.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yyfax.commons.json.JacksonUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.pay.common.constants.CacheName;
import com.yyfax.pay.config.data.dao.MerchantConfigDao;
import com.yyfax.pay.config.data.service.MerchantConfigService;
import com.yyfax.pay.config.model.po.MerchantConfigPO;

@Service
public class MerchantConfigServiceImpl extends BaseServiceImpl implements MerchantConfigService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private MerchantConfigDao merchantConfigDao;

	public MerchantConfigServiceImpl(@Qualifier("payCacheService") CacheService cacheService) {
		super(cacheService);
	}

	@Override
	public int save(MerchantConfigPO config) throws Exception {
		if (config == null) {
			logger.error("新增商户信息传入参数为空，返回-1");
			return -1;
		}
		clearCache(CacheName.MERCHANT_CONFIG);
		return merchantConfigDao.insert(config);
	}

	@Override
	public int modify(MerchantConfigPO config) throws Exception {
		if (config == null) {
			logger.error("修改商户信息传入参数为空，返回-1");
			return -1;
		}
		// 更新商户刷新缓存
		clearCache(CacheName.MERCHANT_CONFIG);
		return merchantConfigDao.update(config);
	}

	@Override
	public MerchantConfigPO queryById(int id) throws Exception {
		if (id <= 0) {
			logger.error("根据id查询商户信息id为{}，id错误", id);
			return null;
		}
		return merchantConfigDao.selectById(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantConfigPO> queryForList() throws Exception {
		// 从缓存读取数据
		List<MerchantConfigPO> pos = null;
		String json = getCache(CacheName.MERCHANT_CONFIG);
		if (StringUtils.isNotBlank(json)) {
			pos = (List<MerchantConfigPO>) JacksonUtil.fromJson(json, List.class, MerchantConfigPO.class);
			return pos;
		}
		logger.info("读取缓存数据为空从数据库获取商户信息");
		pos = merchantConfigDao.selectForList();
		if (pos != null && !pos.isEmpty()) {
			cacheService.set(CacheName.MERCHANT_CONFIG, JacksonUtil.toJson(pos));
		}
		return pos;
	}

	@Override
	public MerchantConfigPO queryByMerchantCode(String merchantCode) throws Exception {
		if (StringUtils.isBlank(merchantCode)) {
			logger.error("根据merchantCode查询商户信息merchantCode为空");
			return null;
		}

		String json = getCache(CacheName.MERCHANT_CONFIG);
		if (StringUtils.isNotBlank(json)) {
			List<MerchantConfigPO> list = (List<MerchantConfigPO>) JacksonUtil.fromJson(json, List.class, MerchantConfigPO.class);
			for (MerchantConfigPO po : list) {
				if (merchantCode.equals(po.getMerchantCode())) {
					return po;
				}
			}
		} else {
			queryForList();
		}
		logger.info("读取内存数据为空从数据库获取商户信息");
		return merchantConfigDao.selectByMerchantCode(merchantCode);
	}
}
